//
//  AppDelegate.swift
//  Sina
//
//  Created by yuanyuan on 15/12/17.
//  Copyright © 2015年 yuanyuan. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        
//        获取数据
        let account = UserAccount.loadAccount()
        
        print("\(account)----------")
//        不使用storyboard  window没有被实例化出来  不存在window
//        需要程序员手动实例化
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        
//        设置主题色
        setThemeColor()
        
        //注册通知
        registerNotification()
        
//        设置根视图控制器
        window?.rootViewController = defaultViewController()     //NewFeatureCollectionViewController()  //MainViewController()
        
        
        return true
    }

    //注册通知
    private func registerNotification() {
        
        //单例对象 程序一运行 就会简历 Appdelegate单例对象  直到程序销毁的时候 才会调用
        //写与不写 没有本质上的区别  只是一种编码习惯而已
        //注册 切换根视图控制器的通知
        
        //object 如果不为空: 通知  ,  nil: 对应广播
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootVC:", name: WBSwitchRootVCNotification, object: nil)
    }
    
    //移除通知的操作
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    //只要是OC中 target - action 这种消息机制 seletor类型都需要使用 @objc 来兼容
    //1.按钮的点击事件
    //2.通知的响应事件
    //3.代理的可选协议方法
    @objc private func switchRootVC(n: NSNotification) {
        
        print(n)
        
        //根据通知去切换对应根视图控制器
        if  n.object != nil {
            //跳转到welcome
            window?.rootViewController = WelcomeViewController()
            return
        }
        
        //首页
        window?.rootViewController = MainViewController()
    }
    
    //根据用户是否登录显示具体的页面
    private func defaultViewController() -> UIViewController {
        //根据用户是否登录
        if UserAccountViewModel().userLogin {
            //需要根据是否是新版本 显示对应页面
            //            if isNewVersion() {
            //                //是新版本
            //                return NewFeatureViewController()
            //            }
            //            return WelcomeViewController()
            return isNewVersion() ? NewFeatureCollectionViewController() : WelcomeViewController()
        }
        //用户没有登录下
        return MainViewController()
    }
    
    // MARK:判断是否是新版本
    private func isNewVersion() -> Bool {
        
        //1.获取当前应用的版本号
        let info = NSBundle.mainBundle().infoDictionary
        let currentStr = info!["CFBundleShortVersionString"] as! String
        let currentNum = Double(currentStr)
        print(currentNum)
        //2.获取本地缓存的版本
        //用userDefaults 来存储版本
        let userDefaults = NSUserDefaults.standardUserDefaults()
        let sandBoxKey = "sandBoxKey"
        let lastNum = userDefaults.doubleForKey(sandBoxKey)
        
        //3. 立即存储 当前应用的版本号
        userDefaults.setDouble(currentNum!, forKey: sandBoxKey)
        userDefaults.synchronize()
        //4.比较新版(当前版本) 版本号 和老版本(本地缓存的版本)的版本号
        
        return currentNum! > lastNum
    }

    
   private func setThemeColor(){
//    一定需要尽早设置
        UINavigationBar.appearance().tintColor = themeColor   
        UITabBar.appearance().tintColor = themeColor
    
    }
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

